Skip to content

PYTHON-4745 - Test behavior of async task cancellation #2136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 10, 2025

Conversation

NoahStapp
Copy link
Contributor

…ation

@NoahStapp NoahStapp requested a review from ShaneHarvey February 6, 2025 17:37

class TestAsyncCancellation(AsyncIntegrationTest):
async def test_async_cancellation_closes_connection(self):
client = await self.async_rs_or_single_client(maxPoolSize=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all of these tests can reuse the global test client instead of creating new ones.

@@ -391,7 +391,7 @@ async def try_next(self) -> Optional[_DocumentType]:
if not _resumable(exc) and not exc.timeout:
await self.close()
raise
except Exception:
except BaseException:
Copy link
Member

@ShaneHarvey ShaneHarvey Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everywhere we catch BaseException, can you add a one line comment to explain it's intentional? Something like:

# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.

await connected(self.client)
conn = one(pool.conns)
await self.client.db.test.insert_one({"x": 1})
self.addAsyncCleanup(self.client.db.test.drop)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use delete_many() instead of drop() for better perf.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better yet, we can use setUpClass/tearDownClass to insert/drop the collection only once.

Copy link
Contributor Author

@NoahStapp NoahStapp Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no asynchronous setUpClass/tearDownClass equivalent in unittest sadly. We could create our own, something like this:

async def setup_class(self):
	...
	self.init_class = True

async def asyncSetup(self):
    ...
    if not self.init_class:
         await self.setup_class()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right. Let's not do that because it worth be hard/impossible to do the tearDownClass part.

async def test_async_cancellation_closes_cursor(self):
await connected(self.client)
for _ in range(2):
await self.client.db.test.insert_one({"x": 1})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below, use insert_many instead of insert_one in a for-loop for better perf.

class TestAsyncCancellation(AsyncIntegrationTest):
async def test_async_cancellation_closes_connection(self):
pool = await async_get_pool(self.client)
await connected(self.client)
Copy link
Member

@ShaneHarvey ShaneHarvey Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove connected from these tests for better perf. If we need to get the connection we can move this below the insert_one().

@ShaneHarvey ShaneHarvey changed the title PYTHON-4745 - Document and Test Behavior when User Cancels Async Oper… PYTHON-4745 - Test behavior of async task cancellation Feb 10, 2025
@NoahStapp NoahStapp merged commit b94dd8e into mongodb:master Feb 10, 2025
42 of 44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants